package cn.ainit.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author JiaJie
 * @create 2018-03-07 1:32 PM
 **/
public class DateUtils {

    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 时间转为字符串
     *
     * @param date  Date
     * @return yyyy-MM-dd HH:mm:ss格式
     */
    public static  String date2String(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
        formatter.setTimeZone(TimeZone.getDefault());
        return formatter.format(date);
    }

    /**
     * 时间转为字符串
     *
     * @param date  Date
     * @param pattern 时间样式
     * @return  String
     */
    public static  String date2String(Date date, String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        formatter.setTimeZone(TimeZone.getDefault());
        return formatter.format(date);
    }

    /**
     * 增加一个月
     * @param date  Date
     * @param pattern   yyyy-MM-dd HH:mm:ss格式
     * @return  String
     */
    public static String plusOneMonth(Date date, String pattern){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 1);
        return DateUtils.date2String(cal.getTime(), pattern);
    }

    public static String plusOneMonthEnd(Date date, String pattern){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 1);
        //得到月末
        cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return DateUtils.date2String(cal.getTime(), pattern);
    }

    /**
     * 先增加一个月，然后再减去一天
     * @param date      Date
     * @param pattern
     * @return
     */
    public static String plusOneMonthAndSubtractOneDay(Date date, String pattern){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 1);
        cal.add(Calendar.DATE, -1);
        return DateUtils.date2String(cal.getTime(), pattern);
    }

    public static String plusOneMonthEndAndSubtractOneDay(Date date, String pattern){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 1);

        //得到月末
        cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));

        cal.add(Calendar.DATE, -1);
        return DateUtils.date2String(cal.getTime(), pattern);
    }

    /**
     * 得到月末
     * @param date      Date
     * @param pattern   pattern
     * @return
     */
    public static String thisMonthEnd(Date date, String pattern){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        //得到月末
        cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return DateUtils.date2String(cal.getTime(), pattern);
    }

    /**
     * 减去一天
     * @param date      Date
     * @param pattern   yyyy-MM-dd HH:mm:ss格式
     * @return          String
     */
    public static String subtractOneDay(Date date, String pattern){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -1);
        return DateUtils.date2String(cal.getTime(), pattern);
    }

    public static void main(String[] args) {
//        Calendar cal = Calendar.getInstance();
//        cal.setTime(new Date());
//        cal.add(Calendar.MONTH, 1);
//        System.out.println(DateUtils.date2String(cal.getTime(), "yyyy-MM-dd"));
        System.out.println(DateUtils.plusOneMonth(new Date(), "yyyy-MM-dd"));
    }
}
